package cn.zzf.algs.note.base.collection.v1;

import edu.princeton.cs.algs4.StdOut;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * @author GaoFeng
 * @date 2019/11/21 17:06
 *
 * 使用单向链表实现的栈
 */
public class LinkedListQueue<T> implements Iterable<T>, Queue<T> {

    /** 当前队列的长度 */
    private int size;
    /** 队首 */
    private Node<T> first;
    /** 队尾 */
    private Node<T> last;

    private Integer[][] sites = new Integer[][]{
            {-1, -1},
            {0, -1},
            {1, -1},
            {-1, 0},
            {1, 0},
            {-1, 1},
            {0, 1},
            {1,1}
    };

    public LinkedListQueue() {
        this.size = 0;
        this.first = null;
        this.last = null;
    }

    @Override
    public void enqueue(T element) {

        Node<T> next = new Node<>(element, null);

        if(first == null) {
            // 初始化首尾节点
            first = next;
            last = first;
        } else {
            // 在尾部添加元素
            Node tempNode = this.last;
            tempNode.next = next;
            this.last = next;
        }

        size++;

    }

    @Override
    public T dequeue() {

        if (first == null) {
            return null;
        }

        T element = first.value;
        first = first.next;

        return element;
    }

    @Override
    public boolean isEmpty() {
        return first == null;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            /** 当前节点 */
            private Node<T> currentNode = first;

            @Override
            public boolean hasNext() {
                return currentNode != null;
            }

            @Override
            public T next() {

                if (currentNode != null) {
                    T element = currentNode.value;
                    currentNode = currentNode.next;
                    return element;
                }

                return null;
            }
        };
    }

    @Override
    public void forEach(Consumer<? super T> action) {

    }

    @Override
    public Spliterator<T> spliterator() {
        return null;
    }

    public Node<T> first() {
        return first;
    }

    public T remove(int index) {

        // 最后导致

        if (index >= size) {
            throw new ArrayIndexOutOfBoundsException(index);
        }

        if (size == 1) {
            T value = first.value;
            first = null;
            size--;
            return value;
        }

        int currentIndex = 0;
        Node<T> currentNode = first;

        while (++currentIndex < index) {
            currentNode = currentNode.next;
        }

        //
        StdOut.println(currentIndex);
        StdOut.println(currentNode);

        T removeValue = currentNode.next.value;
        currentNode.next = currentNode.next.next;

        // 减少容量，size--
        size--;

        return removeValue;

    }

    public void question() {

        /*
            如果活细胞周围八个位置的活细胞数少于两个，则该位置活细胞死亡；
            如果活细胞周围八个位置有两个或三个活细胞，则该位置活细胞仍然存活；
            如果活细胞周围八个位置有超过三个活细胞，则该位置活细胞死亡；
            如果死细胞周围正好有三个活细胞，则该位置死细胞复活；
        */
        /*输入:
        [
          [0,1,0],
          [0,0,1],
          [1,1,1],
          [0,0,0]
        ]
        输出:
        [
          [0,0,0],
          [1,0,1],
          [0,1,1],
          [0,1,0]
        [-1, -1],[0, -1],[1, -1],[-1, 0][1, 0][-1, 1][0,1][1,1]
         */


        int liveCount = 0;
        for (int i = 0;i< sites.length;i++) {
           Integer[] points = sites[i];
           if (points[i] + i == 0) {
           }
        }

    }

}


